package com.hxgis.common.base;

import com.hxgis.common.entity.Station;
import com.hxgis.common.utils.DistanceUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class StationCache {

    private final static BigDecimal DISTANCE = new BigDecimal(200);

    private static List<Station> stations;

    private static Map<String, Station>  STATION_INFO_MAP = new HashMap<String, Station>();
    /**
     * 站点范围的站点
     */
    private static Map<String,StationAssociated> STATION_SCOPE = new HashMap<String,StationAssociated>();


    private static Map<String,StationAssociated> STATION_SCOPE_350 = new HashMap<String,StationAssociated>();

    public static void init(List<Station> stationList){
        stations = stationList;
        initMap();
        //200km站点
        initStationScope();
        //350km站点
        initStationScope350();
    }

    /**
     * 初始化 站点信息
     */
    private static void initMap(){
        for (Station station : stations) {
            STATION_INFO_MAP.put(station.getStationNo(),station);
        }
        log.info("站点基础信息初始化完成");
    }

    /**
     * 计算站点范围类的站点 200km
     */
    private static void initStationScope(){
        for (Station station : stations) {
            StationAssociated stationAssociated = new StationAssociated(station);
            for (Station stationCount : stations) {
                if (station.getStationNo().endsWith(stationCount.getStationNo()))continue;
                //进行计算是否在范围内
                if (compareDistance(station, stationCount, new BigDecimal(200)))
                    stationAssociated.add(stationCount);
            }
            STATION_SCOPE.put(station.getStationNo(),stationAssociated);
        }
        log.info("站点范围信息,周边200 初始化完成");
    }


    /**
     * 计算站点范围类的站点 350km
     */
    private static void initStationScope350(){
        for (Station station : stations) {
            StationAssociated stationAssociated = new StationAssociated(station);
            for (Station stationCount : stations) {
                if (station.getStationNo().endsWith(stationCount.getStationNo()))continue;
                //进行计算是否在范围内
                if (compareDistance(station, stationCount, new BigDecimal(350)))
                    stationAssociated.add(stationCount);
            }
            STATION_SCOPE_350.put(station.getStationNo(),stationAssociated);
        }
        log.info("站点范围信息,周边350 初始化完成");
    }

    /**
     * 比较两个站点是否在200公里的范围内
     * @param station
     * @param stationCount
     * @return
     */
    private static boolean compareDistance(Station station,Station stationCount, BigDecimal distance){
        double v = DistanceUtil.countDistance
                (station.getLon(), station.getLat(), stationCount.getLon(), stationCount.getLat());
        BigDecimal bigDecimal = new BigDecimal(v);
        if (bigDecimal.compareTo(distance) <=0){
            return true;
        }
        return false;
    }

    public static StationAssociated getScopeStationList(String stationNo) {
        return STATION_SCOPE.get(stationNo);
    }

    public static StationAssociated getScopeStation350List(String stationNo) {
        return STATION_SCOPE_350.get(stationNo);
    }

    /**
     * 站点信息缓存
     * @return
     */
    public static Map<String, Station> getStationInfoMap() {
        return STATION_INFO_MAP;
    }

    public static List<Station> getStations() {
        return stations;
    }

}
